/*
 * Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.changil.nowonchangilchurch.page;

import java.util.ArrayList;

import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.changil.nowonchangilchurch.R;
import com.changil.nowonchangilchurch.protocol.ArticleManager;
import com.changil.nowonchangilchurch.protocol.dto.ArticleList;
import com.changil.nowonchangilchurch.protocol.http.DataLoadListener;
import com.changil.nowonchangilchurch.ui.item.ArticleItem;
import com.changil.nowonchangilchurch.ui.layout.ArticleImageViewPager;
import com.changil.nowonchangilchurch.ui.layout.ArticleListView;
import com.changil.nowonchangilchurch.ui.layout.CommonListView;
import com.changil.nowonchangilchurch.ui.layout.CommonViewPager;
import com.changil.nowonchangilchurch.ui.layout.ScaleableImageViewerDialog;

public class ArticleListFragment extends BaseNetworkFragment {
	private int mBoardId = 0;
	private int mFirsttArticleId = 0;
	private int mLastArticleId = 0;
	private ArticleListView mList = null;
	private boolean mIsLastShow = false;
	private final int ARTICLE_LOAD_COUNT = 20;
	private String mHeaderTitle = null;
	private String mHeaderUrl = null;
	
	public ArticleListFragment() {
		
	}
	
	private ArticleListFragment(int boardId){
		mBoardId = boardId;
	}
	
	public static ArticleListFragment newInstance(int boardId) {
		ArticleListFragment f = new ArticleListFragment(boardId);
		return f;
	}

	@Override
	protected View createContentView(LayoutInflater inflater) {
		View view = inflater.inflate(R.layout.fragment_article_list, null);
		mList = (ArticleListView)view.findViewById(R.id.fragment_article_list);
		mList.setUseBusyMode(false);
		mList.setUserActionListener(mUserActionListener);
		mList.setInnerListViewUserActionListener(mInnerListViewUserActionListener);
		if(null != mHeaderTitle) {
			ArticleItem header = new ArticleItem(getActivity());
			if(null != mHeaderUrl) {
				header.setArticleTitle(mHeaderTitle+"("+mHeaderUrl+")");
				header.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						if(!mHeaderUrl.startsWith("http://")){
							mHeaderUrl = "http://"+mHeaderUrl;
						}
						Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse(mHeaderUrl));
						startActivity(i);
					}
				});
			} else {
				header.setArticleTitle(mHeaderTitle);
			}
			mList.addHeaderView(header);
		}
		return view;
	}
	
	@Override
	protected void loadData() {
		ArticleManager.getsInstance().getArticleList(mDataLoadListener,mBoardId,Integer.MAX_VALUE, ARTICLE_LOAD_COUNT, true);
	}
	
	private void loadDataNew() {
		ArticleManager.getsInstance().getArticleList(mDataLoadNewListener,mBoardId,mFirsttArticleId, Integer.MAX_VALUE, false);
	}
	
	private void loadDataMore() {
		ArticleManager.getsInstance().getArticleList(mDataLoadMoreListener,mBoardId,mLastArticleId, ARTICLE_LOAD_COUNT, true);
	}
	
	private DataLoadListener<ArticleList> mDataLoadListener = new DataLoadListener<ArticleList>() {
		
		@Override
		public void onSuccess(ArticleList response) {
			setLoadingView(false);
			if(0 < response.size) {
				mList.addList(response.list);
				mFirsttArticleId = response.list.get(0).id;
				mLastArticleId = response.list.get(response.size -1).id;
			} else {
				mList.setEmptyMessage(getString(R.string.common_empty_article));
			}
			mList.setAdapter();
		}
		
		@Override
		public void onFail() {
			setLoadingView(false);
			setRetryView(true);
		}
	};
	
	private DataLoadListener<ArticleList> mDataLoadNewListener = new DataLoadListener<ArticleList>() {
		
		@Override
		public void onSuccess(ArticleList response) {
			if(0 < response.size) {
				mList.addFirstList(response.list);
				mFirsttArticleId = response.list.get(0).id;
				mList.notifyDataSetChanged();
			} else {
				mList.notifyRefreshComplete();
			}
		}
		
		@Override
		public void onFail() {
			Toast.makeText(getActivity(), getString(R.string.common_error), Toast.LENGTH_SHORT).show();
			mList.notifyRefreshComplete();
		}
	};
	
	private DataLoadListener<ArticleList> mDataLoadMoreListener = new DataLoadListener<ArticleList>() {
		
		@Override
		public void onSuccess(ArticleList response) {
			if(0 < response.size) {
				mList.addList(response.list);
				mLastArticleId = response.list.get(response.size -1).id;
				mList.notifyDataSetChanged();
			} else {
				mList.removeFooterLodingView();
				mIsLastShow = true;
			}
		}
		
		@Override
		public void onFail() {
			Toast.makeText(getActivity(), getString(R.string.common_error), Toast.LENGTH_SHORT).show();
			mList.notifyRefreshComplete();
		}
	};
	
	private CommonListView.UserActionListener mUserActionListener = new CommonListView.UserActionListener() {
		public void onItemClick(String id, View v) {
			
		}
		public void onBottomRefresh() {
			if(!mIsLastShow) {
				loadDataMore();
			}
		}
		public void onTopRefresh() {
			loadDataNew();
		}
	};
	
	private ArticleImageViewPager.UserActionListener mInnerListViewUserActionListener= new ArticleImageViewPager.UserActionListener() {
		
		@Override
		public void onItemClick(View v, int position, ArrayList<String> imageIdList, String articleId) {
			ScaleableImageViewerDialog dialog = new ScaleableImageViewerDialog(getActivity());
			dialog.addList(imageIdList);
			dialog.setCurrentItem(position);
			dialog.setParentViewPager((CommonViewPager)v);
			dialog.show();
		}
		
		@Override
		public void onPageScrolled(int position, boolean isLastPage) {
		}
		
	};
	
	public void setHeader(String title, String url) {
		mHeaderTitle = title;
		mHeaderUrl = url;
	}
	
}